layui.config({
    base: '/static/treetable-lay/'
}).use(['form','layer','jquery','treetable','table','laydate','upload','element'],function(){
    var form = layui.form,
        layer = layui.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table=layui.table,
        upload = layui.upload,
        laydate=layui.laydate,
        element=layui.element,
        treetable = layui.treetable;
    
    var gMenuId = undefined;
    var gMenuUrl = undefined;
    var gMenuType = undefined;
    // 渲染表格
    function reload(){
    	treetable.render({
    		treeColIndex: 1,
    		treeSpid: -1,
    		treeIdName: 'id',
    		treePidName: 'pid',
    		treeDefaultClose: false,
    		treeLinkage: false,
    		elem: '#menuTable',
    		url: '/menu/menuList',
    		page: false,
    		height: 'full-30',
    		cols: [[
    			{type: 'radio',width:"5%"},
    			{field: 'menuName', title: '菜单名称',width:"45%"},
    			{field: 'menuType', title: '类型',width:"10%"},
    			{field: 'menuUrl', title: '接口URL（不为空即为页面）',width:"30%"},
    			{field: 'menuIcon', title: '图标',width:"10%",templet:function(d){
    				return "<i class='layui-icon'>"+(d.menuIcon==null?"":d.menuIcon)+"</i>"
    			}}
    			]],
    			defaultToolbar: [],
    			toolbar: '#menuTableToolbar'
    	});
    }
    reload();
	table.on('toolbar(menuTable)', function(obj){
    	var tableCheckData = table.checkStatus(obj.config.id).data;
		switch(obj.event){
		    case 'add_base':
		    	var index = layer.open({
		    		type: 2
		    		,title: "新增根节点"
		    		,maxmin: true
		    	  	,area: ['40%', '60%']
		    	  	,content: '/menu/addHtml'
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var supperId = body.find("#supperId").val();
		        		var menuName = body.find("#menuName").val();
		        		var menuType = body.find("#menuType").val();
		        		var menuUrl = body.find("#menuUrl").val();
		        		var icon = body.find("#icon").html();
		        		var orderByIndex = body.find("#orderByIndex").val();
		        		var reg = /^\d+(\.\d+)?$/;
		        		
		        		if(menuName == ''){
		        			layer.msg("菜单名称不能为空",{icon:2});
		        			return;
		        		}
		        		if(icon == ''){
		        			layer.msg("图标不能为空",{icon:2});
		        			return;
		        		}
		        		if(orderByIndex == ''){
		        			layer.msg("排序不能为空",{icon:2});
		        			return;
		        		}
		        		if(!reg.exec(orderByIndex)){
		        			layer.msg("排序必须为数字!");
		        			return;
		        		}
		        		var addData={
		        			supperId:supperId,
		        			menuName:menuName,
		        			menuType:"菜单",
		        			menuUrl:menuUrl,
		        			menuIcon:icon,
		        			orderByIndex:orderByIndex
		        		}
		        		
		        		$.post('/menu/add',addData,function(d){
		        			if(d.code == '200'){
		        				top.layer.msg("新增成功",{icon:1});
								layer.close(index);
							}else{
								layer.msg("新增失败，"+d.msg,{icon:2});
							}
		        			reload();
		        		});
		        	}
	        	})
        	break;
		    case 'add_sub':
		    	if(gMenuId==undefined){
		    		layer.msg("请选择节点");
		    		return;
		    	}
		    	if(gMenuUrl!=undefined && gMenuUrl!=''){
		    		layer.msg("无法在页面节点下添加子节点");
		    		return;
		    	}
		    	if(gMenuType!=undefined && gMenuType=="按钮"){
		    		layer.msg("无法在类型为按钮下添加子节点");
		    		return;
		    	}
		    	var index = layer.open({
		    		type: 2
		    		,title: "新增子节点"
		    		,maxmin: true
		    	  	,area: ['40%', '60%']
		    	  	,content: '/menu/addHtml?id='+gMenuId
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var supperId = body.find("#supperId").val();
		        		var menuName = body.find("#menuName").val();
		        		var menuType = body.find("#menuType").val();
		        		var menuUrl = body.find("#menuUrl").val();
		        		var icon = body.find("#icon").html();
		        		var orderByIndex = body.find("#orderByIndex").val();
		        		var reg = /^\d+(\.\d+)?$/;
		        		
		        		if(menuName == ''){
		        			layer.msg("菜单名称不能为空",{icon:2});
		        			return;
		        		}
		        		if(icon == ''){
		        			layer.msg("图标不能为空",{icon:2});
		        			return;
		        		}
		        		if(orderByIndex == ''){
		        			layer.msg("排序不能为空",{icon:2});
		        			return;
		        		}
		        		if(!reg.exec(orderByIndex)){
		        			layer.msg("排序必须为数字!");
		        			return;
		        		}
		        		var addData={
		        			supperId:supperId,
		        			menuName:menuName,
		        			menuType:"菜单",
		        			menuUrl:menuUrl,
		        			menuIcon:icon,
		        			orderByIndex:orderByIndex
		        		}
		        		
		        		$.post('/menu/add',addData,function(d){
		        			if(d.code == '200'){
		        				top.layer.msg("新增成功",{icon:1});
								layer.close(index);
								gMenuId = undefined;
								gMenuUrl = undefined;
								gMenuType = undefined;
							}else{
								layer.msg("新增失败，"+d.msg,{icon:2});
							}
		        			reload();
		        		});
		        	}
	        	})
        	break;
		    case 'add_btn':
		    	if(gMenuId==undefined){
		    		layer.msg("请选择节点");
		    		return;
		    	}
		    	if(gMenuUrl==undefined || gMenuUrl==''){
		    		layer.msg("只能在页面节点下添加按钮");
		    		return;
		    	}
		    	var index = layer.open({
		    		type: 2
		    		,title: "新增按钮"
		    		,maxmin: true
		    	  	,area: ['40%', '60%']
		    	  	,content: '/menu/addBtnHtml?id='+gMenuId
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var supperId = body.find("#supperId").val();
		        		var id = body.find("#btnId").val();
		        		var menuName = body.find("#btnName").val();
		        		
		        		if(id == ''){
		        			layer.msg("按钮ID不能为空",{icon:2});
		        			return;
		        		}
		        		if(menuName == ''){
		        			layer.msg("按钮名称不能为空",{icon:2});
		        			return;
		        		}
		        		var addData={
		        			supperId:supperId,
		        			id:id,
		        			menuName:menuName,
		        			menuType:"按钮"
		        		}
		        		
		        		$.post('/menu/add',addData,function(d){
		        			if(d.code == '200'){
		        				top.layer.msg("新增成功",{icon:1});
								layer.close(index);
								gMenuId = undefined;
								gMenuUrl = undefined;
								gMenuType = undefined;
							}else{
								layer.msg("新增失败，"+d.msg,{icon:2});
							}
		        			reload();
		        		});
		        	}
	        	})
        	break;
		    case 'edit':
		    	if(gMenuId==undefined){
		    		layer.msg("请选择修改数据");
		    		return;
		    	}
		    	if(gMenuType!=undefined && gMenuType=="按钮"){
		    		var index = layer.open({
		    			type: 2
		    			,title: "修改"
	    				,maxmin: true
	    				,area: ['40%', '60%']
			    		,content: '/menu/editBtnHtml?id='+gMenuId
			    		,btn: ['保存','返回']
			    		,yes: function(index, layero){
			    			var body = layer.getChildFrame('body', index);
			    			
			    			var supperId = body.find("#supperId").val();
			        		var id = body.find("#btnId").val();
			        		var menuName = body.find("#btnName").val();
			        		if(id == ''){
			    				layer.msg("按钮ID不能为空",{icon:2});
			    				return;
			    			}
			        		if(menuName == ''){
			    				layer.msg("按钮名称不能为空",{icon:2});
			    				return;
			    			}
			        		var editData={
		    					id:id,
		    					supperId:supperId,
		    					menuName:menuName,
		    					menuType:"按钮"
			    			}
			        		$.post('/menu/editBtn',{data:JSON.stringify(editData),id:gMenuId},function(d){
			    				if(d.code == '200'){
			    					top.layer.msg("修改成功",{icon:1});
			    					layer.close(index);
			    					gMenuId = undefined;
			    					gMenuType = undefined;
			    				}else{
			    					layer.msg("修改失败，"+d.msg,{icon:2});
			    				}
			    				reload();
			    			});
			    		}
		    		})
		    	}else{
		    		var index = layer.open({
		    			type: 2
		    			,title: "修改"
	    				,maxmin: true
	    				,area: ['40%', '60%']
			    		,content: '/menu/editHtml?id='+gMenuId
			    		,btn: ['保存','返回']
			    		,yes: function(index, layero){
			    			var body = layer.getChildFrame('body', index);
			    			
			    			var supperId = body.find("#supperId").val();
			    			var menuName = body.find("#menuName").val();
			    			var menuType = body.find("#menuType").val();
			    			var menuUrl = body.find("#menuUrl").val();
			    			var icon = body.find("#icon").html();
			    			var orderByIndex = body.find("#orderByIndex").val();
			        		var reg = /^\d+(\.\d+)?$/;
			    			
			    			if(menuName == ''){
			    				layer.msg("菜单名称不能为空",{icon:2});
			    				return;
			    			}
			    			if(icon == ''){
			    				layer.msg("图标不能为空",{icon:2});
			    				return;
			    			}
			    			console.log(orderByIndex);
			    			if(orderByIndex == ''){
			        			layer.msg("排序不能为空",{icon:2});
			        			return;
			        		}
			        		if(!reg.exec(orderByIndex)){
			        			layer.msg("排序必须为数字!");
			        			return;
			        		}
			    			var editData={
			    					id:gMenuId,
			    					supperId:supperId,
			    					menuName:menuName,
			    					menuType:"菜单",
			    					menuUrl:menuUrl,
			    					menuIcon:icon,
				        			orderByIndex:orderByIndex
			    			}
			    			
			    			$.post('/menu/edit',editData,function(d){
			    				if(d.code == '200'){
			    					top.layer.msg("修改成功",{icon:1});
			    					layer.close(index);
			    					gMenuId = undefined;
			    					gMenuType = undefined;
			    				}else{
			    					layer.msg("修改失败，"+d.msg,{icon:2});
			    				}
			    				reload();
			    			});
			    		}
		    		})
		    	}
        	break;
		};
	})
	table.on('row(menuTable)', function(obj){
    	obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');//选中行样式
        obj.tr.find('input[lay-type="layTableRadio"]').prop("checked",true);
        form.render('radio');
    	gMenuId = obj.data.id;
    	gMenuUrl = obj.data.menuUrl;
    	gMenuType = obj.data.menuType;
	});
})
